﻿using System;
using System.Net;
using AVL;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AVLTutorial
{
    public class AVLTreeWrapper
    {
        AVLTree.AVLTree<int> FSharpTree = AVLTree.empty<int>();
        public AVLTreeWrapper(int i)
        {
            this.Add(i);
        }
        private static BinaryTreeNode toBinaryTreeNode(AVLTree.AVLTree<int> tr)
        {
            if(AVLTree.isEmpty(tr))
            {
                return null;
            }
            else
            {
                BinaryTreeNode btree = new BinaryTreeNode(AVLTree.rootValue(tr));
                btree.leftChildNode = toBinaryTreeNode(AVLTree.left(tr));
                btree.rightChildNode = toBinaryTreeNode(AVLTree.right(tr));
                return btree;
            }
        }
        public BinaryTreeNode toBinaryTreeNode() { return AVLTreeWrapper.toBinaryTreeNode(FSharpTree); }
        public BinaryTreeNode Add(int i)
        {
            FSharpTree = AVLTree.insert<int>(i, FSharpTree);
            return this.toBinaryTreeNode();
        }
        public BinaryTreeNode Remove(int i)
        {
            FSharpTree = AVLTree.delete<int>(i, FSharpTree);
            return this.toBinaryTreeNode();
        }
        public System.Collections.Generic.IEnumerable<BinaryTreeNode> InstructiveAdd(int i)
        {
            System.Collections.Generic.IEnumerable<AVLTree.AVLTree<int>> stepIter = AVLTree.insertIter<int>(i,FSharpTree);
            FSharpTree = AVLTree.insert<int>(i,FSharpTree);
            foreach(AVLTree.AVLTree<int> x in stepIter)
            {
                yield return toBinaryTreeNode(x);
            }
        }
        public System.Collections.Generic.IEnumerable<BinaryTreeNode> InstructiveRemove(int i)
        {
            System.Collections.Generic.IEnumerable<AVLTree.AVLTree<int>> stepIter = AVLTree.deleteIter<int>(i, FSharpTree);
            FSharpTree = AVLTree.delete<int>(i, FSharpTree);
            foreach (AVLTree.AVLTree<int> x in stepIter)
            {
                yield return toBinaryTreeNode(x);
            }
        }
    }
}
